/*
 * JaLingo, http://jalingo.sourceforge.net/
 *
 * Copyright (c) 2002-2006 Oleksandr Shyshko
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uwncorrector.client.engine.dictionaryindex.reader;

import uwncorrector.centre.util.assertions.States;
import uwncorrector.centre.util.io.ByteArray;
import uwncorrector.centre.util.io.nio.MappedByteBufferWrapper;

import java.io.IOException;
import java.nio.MappedByteBuffer;

public class DataSource implements IDataSource {
    private MappedByteBufferWrapper bufferWrapper;
    private MappedByteBuffer buffer;

    public DataSource( String dataFileName ) throws IOException {
        this.bufferWrapper = new MappedByteBufferWrapper( dataFileName );
        this.buffer = bufferWrapper.getMappedByteBuffer();
    }

    public void getData( int start, int length, ByteArray byteArray ) {
        byteArray.setLength( length );

        this.buffer.position( start );
        this.buffer.get( byteArray.getBytes(), 0, length );
    }

    public void close() throws IOException {
        States.assertNotNull( bufferWrapper, "DataSource expected to be not closed" );
        bufferWrapper.close();
        bufferWrapper = null;
    }
}
